{% extends 'adminuser/base_admin.html' %}

{% block admin_title %}操作日志{% endblock %}

{% block admin_content %}
<div class="d-flex justify-content-between mb-3">
    <h5>操作日志</h5>
    <div>
        <form class="d-flex" method="get">
            <div class="input-group me-2">
                <input type="text" name="search" class="form-control" placeholder="搜索日志..." value="{{ search_query }}">
                <button type="submit" class="btn btn-outline-primary">
                    <i class="bi bi-search"></i>
                </button>
            </div>
            <select name="action_type" class="form-select" onchange="this.form.submit()">
                <option value="" {% if not action_type %}selected{% endif %}>所有操作</option>
                <option value="admin_login" {% if action_type == 'admin_login' %}selected{% endif %}>管理员登录</option>
                <option value="admin_logout" {% if action_type == 'admin_logout' %}selected{% endif %}>管理员注销</option>
                <option value="delete_story" {% if action_type == 'delete_story' %}selected{% endif %}>删除故事</option>
                <option value="ban_user" {% if action_type == 'ban_user' %}selected{% endif %}>封禁用户</option>
                <option value="other" {% if action_type == 'other' %}selected{% endif %}>其他操作</option>
            </select>
        </form>
    </div>
</div>

<div class="card">
    <div class="card-header bg-light">
        <div class="row">
            <div class="col-md-2">时间</div>
            <div class="col-md-2">管理员</div>
            <div class="col-md-2">操作类型</div>
            <div class="col-md-4">详情</div>
            <div class="col-md-2">IP地址</div>
        </div>
    </div>
    <div class="card-body p-0">
        <div class="list-group list-group-flush">
            {% for log in page_obj %}
            <div class="list-group-item">
                <div class="row align-items-center">
                    <div class="col-md-2">
                        <small>{{ log.created_at|date:"Y-m-d H:i" }}</small>
                    </div>
                    <div class="col-md-2">
                        <a href="{% url 'adminuser:user_detail' user_id=log.admin.id %}">
                            {{ log.admin.username }}
                        </a>
                    </div>
                    <div class="col-md-2">
                        {% if log.action_type == 'admin_login' %}
                        <span class="badge bg-success">管理员登录</span>
                        {% elif log.action_type == 'admin_logout' %}
                        <span class="badge bg-secondary">管理员注销</span>
                        {% elif log.action_type == 'delete_story' %}
                        <span class="badge bg-danger">删除故事</span>
                        {% elif log.action_type == 'ban_user' %}
                        <span class="badge bg-warning">封禁用户</span>
                        {% else %}
                        <span class="badge bg-info">{{ log.action_type }}</span>
                        {% endif %}
                    </div>
                    <div class="col-md-4">
                        <small>{{ log.description }}</small>
                    </div>
                    <div class="col-md-2">
                        <small>{{ log.ip_address }}</small>
                    </div>
                </div>
            </div>
            {% empty %}
            <div class="list-group-item text-center">
                <p class="text-muted my-3">没有找到符合条件的日志记录</p>
            </div>
            {% endfor %}
        </div>
    </div>
    <div class="card-footer">
        <nav>
            <ul class="pagination justify-content-center mb-0">
                {% if page_obj.has_previous %}
                <li class="page-item">
                    <a class="page-link" href="?page=1{% if search_query %}&search={{ search_query }}{% endif %}{% if action_type %}&action_type={{ action_type }}{% endif %}">首页</a>
                </li>
                <li class="page-item">
                    <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if search_query %}&search={{ search_query }}{% endif %}{% if action_type %}&action_type={{ action_type }}{% endif %}">上一页</a>
                </li>
                {% else %}
                <li class="page-item disabled">
                    <a class="page-link" href="#">首页</a>
                </li>
                <li class="page-item disabled">
                    <a class="page-link" href="#">上一页</a>
                </li>
                {% endif %}
                
                <li class="page-item active">
                    <span class="page-link">{{ page_obj.number }} / {{ page_obj.paginator.num_pages }}</span>
                </li>
                
                {% if page_obj.has_next %}
                <li class="page-item">
                    <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if search_query %}&search={{ search_query }}{% endif %}{% if action_type %}&action_type={{ action_type }}{% endif %}">下一页</a>
                </li>
                <li class="page-item">
                    <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}{% if search_query %}&search={{ search_query }}{% endif %}{% if action_type %}&action_type={{ action_type }}{% endif %}">末页</a>
                </li>
                {% else %}
                <li class="page-item disabled">
                    <a class="page-link" href="#">下一页</a>
                </li>
                <li class="page-item disabled">
                    <a class="page-link" href="#">末页</a>
                </li>
                {% endif %}
            </ul>
        </nav>
    </div>
</div>
{% endblock %} 